package com.weijun.keepalivedemo.daemon

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import com.weijun.keepalive_daemon.DaemonKeepAlive

/**
 * @author: weijun
 * date: 2023/7/10
 * description:
 */
class SystemBroadcastReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context?, intent: Intent?) {
        if (intent == null) {
            return
        }
        when (intent.action) {
            Intent.ACTION_PACKAGE_ADDED, Intent.ACTION_PACKAGE_REMOVED -> {
                startActivity(
                    Intent(DaemonKeepAlive.application?.applicationContext, OnRemoveActivity::class.java)
                )
            }
        }
    }

    private fun startActivity(intent: Intent) {
        DaemonKeepAlive.application?.applicationContext?.let {
            intent.flags = Intent.FLAG_ACTIVITY_NO_ANIMATION
            intent.addCategory("android.intent.category.LAUNCHER")
            intent.action = "android.intent.action.MAIN"
            intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND)
            DaemonKeepAlive.startActivity(it, intent)
        }
    }
}